package com.example.endproject;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity2 extends Activity {
    private static final String TAG = "Register2Activity";
    String userName,pwd;//用户名,密码
    String ID,name,age,department,hobby,constellation,signature;//个人信息
    TextView IDView,nameView,ageView,departmentView,hobbyView,constellationView,signatureView;//控件
    DBHelper dbHelper;
    SQLiteDatabase mSqlDB;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register2);
        TextView subjectView = findViewById(R.id.register2_subject);
        subjectView.setTypeface(Typeface.createFromAsset(getAssets(),"font/FZBangSXJW.ttf"));
        //初始化控件
        IDView = findViewById(R.id.register2_ID);
        nameView = findViewById(R.id.register2_Name);
        ageView = findViewById(R.id.register2_Age);
        departmentView = findViewById(R.id.register2_Department);
        hobbyView = findViewById(R.id.register2_Hobby);
        constellationView = findViewById(R.id.register2_Constellation);
        signatureView = findViewById(R.id.register2_Signature);
        //获取用户名,密码
        Intent intent = getIntent();
        userName = intent.getStringExtra("userName");
        pwd = intent.getStringExtra("pwd");
        dbHelper = new DBHelper(this);
        mSqlDB = dbHelper.getWritableDatabase();
    }
    public void onclickPre(View view){
        finish();
    }
    public void onclickFinish(View view){
        //获得控件中的信息
        ID = IDView.getText().toString();
        name = nameView.getText().toString();
        age = ageView.getText().toString();
        department = departmentView.getText().toString();
        hobby = hobbyView.getText().toString();
        constellation = constellationView.getText().toString();
        signature = signatureView.getText().toString();

        //判断控件中ID和Name信息是否为空,ID是否唯一
        if(ID.equals("")){
            IDView.setHint("不能为空");
            return;
        }
        if (name.equals("")){
            nameView.setHint("不能为空");
            return;
        }
        Cursor query = mSqlDB.query(DBHelper.TB_NAME_MSG,
                new String[]{"ID", "name", "age", "department", "hobby", "constellation", "signature"},
                "ID = ?", new String[]{ID}, null, null, null);
        if (query.moveToFirst()){
            IDView.setHint("请填写自己的学号");
            query.close();
            return;
        }
        query.close();
        //无误后插入数据库中
        //用户表

        try {
            mSqlDB.beginTransaction();
            ContentValues values = new ContentValues();
            values.put("user_name",userName);
            values.put("pwd",pwd);
            mSqlDB.insert(DBHelper.TB_NAME_USER,null,values);
            //信息表
            ContentValues values1 = new ContentValues();
            values1.put("ID",ID);
            values1.put("name",name);
            values1.put("age",age);
            values1.put("department",department);
            values1.put("hobby",hobby);
            values1.put("constellation",constellation);
            values1.put("signature",signature);
            mSqlDB.insert(DBHelper.TB_NAME_MSG,null,values1);
            //关系表
            ContentValues values2 = new ContentValues();
            values2.put("user_name",userName);
            values2.put("ID",ID);
            mSqlDB.insert(DBHelper.TB_NAME_Relate,null,values2);
            mSqlDB.setTransactionSuccessful();
        }catch (SQLException e){
            Log.i(TAG, "onclickFinish: 信息插入失败");
            Toast.makeText(getApplicationContext(),"注册失败",Toast.LENGTH_LONG);
            return;
        }finally {
            mSqlDB.endTransaction();
        }
        Intent intent = new Intent(this,MainActivity.class);
        startActivity(intent);
    }
}